/**
 * @file calculate.h
 * @author 冯皓
 * @brief 
 * @version 0.1
 * @date 2022-12-24
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include <iostream>
#include <stack>
#include <vector>
#include <list>
#include <math.h>
#include <cstdlib>
using namespace std;
void pushstack();
void cal();
int priority(char _ope);
stack<char> ope;
stack<double> num;
int flag=0;//表达式的总字符数
int flag2[200]={0};//判断第n个字符是数字还是符号
int leftnum=0;//栈中左括号的数目用于对“-”运算作出判断
int left1=0;//左括号的数目
int right1=0;//右括号的数目用于判断括号不匹配的情况
double n[100];//用于输出表达式的数组
char c[100];//用于输出表达式的数组
int flag3=0;//用于判断第一个输入的是不是负号
void pushstack(){
    char inputope;
    double inputnum;
    while(1){
        if(cin.peek()>='0'&&cin.peek()<='9'){
            flag++;
            flag2[flag]=0;
            cin>>inputnum;
            n[flag]=inputnum;
            //cout<<inputnum;
            num.push(inputnum);
        }else if(cin.peek()=='\n'){
            if(left1!=right1){
                cout<<"错误：括号不匹配";
                exit(EXIT_FAILURE);
            }
            while(!ope.empty()){
                cal();
            }
            break;
        }else if(cin.peek()=='('){
            flag++;
            leftnum++;
            left1++;
            flag2[flag]=1;
            cin>>inputope;
            c[flag]=inputope;
            //cout<<inputope;
            ope.push(inputope);
        }else if(cin.peek()==')'){
            flag++;
            right1++;
            flag2[flag]=1;
            cin>>inputope;
            c[flag]=inputope;
            //cout<<inputope;
            /**
             * @brief 计算直到栈顶是左括号
             * 
             */
            while(1){
                cal();
                if(ope.empty()){
                    cout<<"错误：括号不匹配";
                    exit(EXIT_FAILURE);
                }
                if(ope.top()=='('){
                    break;
                }   
            }
            ope.pop();
            leftnum--;
        }else if(ope.empty()){
            flag++;
            flag2[flag]=1;
            cin>>inputope;
            c[flag]=inputope;
            ope.push(inputope);
            if(flag==1&&inputope=='-'){
                flag3++;
            }
        }else{
            flag++;
            flag2[flag]=1;
            cin>>inputope;
            c[flag]=inputope;
            if(c[flag-1]=='('&&c[flag]=='-'){
                flag3++;
            }
            /**
             * @brief 若新输入的字符的优先级小于栈顶的优先级，则进行计算
             * 
             */
            while((!ope.empty())&&priority(inputope)<=priority(ope.top())){
                cal();
            }
            ope.push(inputope);
        }
    }
}

void cal(){
    double num1;
    double num2;
    /**
     * @brief 用于判断栈中的‘-’是减号还是负号
     * 
     */
    if(ope.top()=='-'){
        ope.pop();
        num1=num.top();
        num.pop();
        if(!num.empty()){
            if(ope.empty()||(ope.size()-leftnum+1-flag3)==num.size()){
                ope.push('+');
            }
        }
        num.push(num1*(-1));
    }else if(ope.top()=='+'){
        ope.pop();
        num1=num.top();
        num.pop();
        num2=num.top();
        num.pop();
        num.push(num1+num2);
    }else if(ope.top()=='*'){
        ope.pop();
        num1=num.top();
        num.pop();
        num2=num.top();
        num.pop();
        num.push(num1*num2);
    }else if(ope.top()=='/'){
        ope.pop();
        num1=num.top();
        num.pop();
        num2=num.top();
        num.pop();
        if(num1!=0){
            num.push(num2/num1);
        }else{
            cout<<"错误:除数为零";
            exit(EXIT_FAILURE);
        }
    }else if(ope.top()=='^'){
        ope.pop();
        num1=num.top();
        num.pop();
        num2=num.top();
        num.pop();
        num.push(pow(num2,num1));
    // }else if(ope.top()=='('){
    //     cout<<"错误：括号不匹配";
    //     exit(EXIT_FAILURE);
    }else if(ope.top()!='('){
        cout<<"错误：出现非法运算符";
        exit(EXIT_FAILURE);
    }
}

int priority(char _ope){
    if(_ope=='('){
        return 0;
    }else if(_ope=='+'||_ope=='-'){
        return 1;
    }else if(_ope=='*'||_ope=='/'){
        return 2;
    }else if(_ope=='^'){
        return 3;
    }
}